const axios = require('axios');

const BASE_URL = 'http://localhost:3000/api';

async function testAPI(method, endpoint, data = null) {
  try {
    const config = {
      method,
      url: `${BASE_URL}${endpoint}`,
      timeout: 5000
    };
    
    if (data) {
      config.data = data;
    }
    
    const response = await axios(config);
    console.log(`✓ ${method.toUpperCase()} ${endpoint} - ${response.status}`);
    return response.data;
  } catch (error) {
    if (error.response) {
      console.log(`✗ ${method.toUpperCase()} ${endpoint} - ${error.response.status}: ${error.response.statusText}`);
    } else {
      console.log(`✗ ${method.toUpperCase()} ${endpoint} - ${error.message}`);
    }
    return null;
  }
}

async function testAllAPIs() {
  console.log('开始测试所有API接口...\n');
  
  // 测试包相关接口
  console.log('=== 包管理接口 ===');
  await testAPI('GET', '/packages');
  await testAPI('GET', '/packages?query=vue&page=1&pageSize=10');
  await testAPI('GET', '/packages/1');
  
  // 测试标签相关接口
  console.log('\n=== 标签管理接口 ===');
  await testAPI('GET', '/tags');
  await testAPI('GET', '/tags?query=前端');
  await testAPI('GET', '/tags/1');
  
  // 测试讨论相关接口
  console.log('\n=== 讨论接口 ===');
  await testAPI('GET', '/discussions');
  await testAPI('GET', '/discussions?query=vue&packageId=1&sortBy=newest&page=1&pageSize=10');
  await testAPI('GET', '/discussions/package/1');
  
  // 测试统计相关接口
  console.log('\n=== 统计接口 ===');
  await testAPI('GET', '/stats/overview');
  await testAPI('GET', '/stats/download-trend');
  await testAPI('GET', '/stats/top-packages');
  await testAPI('GET', '/stats/tag-distribution');
  await testAPI('GET', '/stats/discussion-activity');
  await testAPI('GET', '/stats/download-details');
  
  // 测试同步相关接口
  console.log('\n=== 同步接口 ===');
  await testAPI('GET', '/sync/status');
  await testAPI('GET', '/sync/config');
  await testAPI('GET', '/sync/history');
  
  // 测试健康检查
  console.log('\n=== 健康检查 ===');
  await testAPI('GET', '/health');
  
  console.log('\nAPI测试完成！');
}

// 等待服务器启动
setTimeout(() => {
  testAllAPIs().catch(console.error);
}, 2000);